package com.zjh.photoshopstudy.Utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class CheckWordUtil {

    private static CheckWordUtil instance;

    private List<String> words = new ArrayList<>();

    public static synchronized CheckWordUtil getInstance(Context context) {
        if (instance == null) {
            instance = new CheckWordUtil(context);
        }
        return instance;
    }

    private CheckWordUtil(Context context) {
        init(context);
    }

    private void init(Context context) {
        try {
            InputStream is = context.getAssets().open("word.xls");
            Workbook workbook = Workbook.getWorkbook(is);//只读取表格不需要获取WritableWorkbook
            Sheet sheet = workbook.getSheet(0);//获取第一张表
            int rowsCount = sheet.getRows();//获取表中的行数
            int cloumsCount = sheet.getColumns();//获取表中的列数
            for (int i = 1; i < rowsCount; i++) {
                Cell cell = sheet.getCell(3, i);//获取第二行第三列的cell对象
                String contents = cell.getContents();//获取cell对象中的内容
                if (!TextUtils.isEmpty(contents)) {
                    Log.d("word", contents);
                    words.add(contents);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
    }

    public boolean checkWord(String str) {
        for (String word : words) {
            if (str.contains(word)) return false;
        }
        return true;
    }

}
